/*
 * =====================================================================================
 *
 *       Filename:  timer.c
 *
 *    Description:  timer
 *
 *        Version:  1.0
 *        Created:  07/05/2012 06:15:46 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Liu XiaoFei (), 
 *        Company:  
 *
 * =====================================================================================
 */
#include    <stdio.h>
#include    <unistd.h>
#include    <sys/time.h>
#include    <signal.h>

int limit = 10;
void timeout_info(int signo)
{
    if (0 == limit)
    {
        printf("time reached\n");
        return;
    }

    printf("%d seconds lef\n", limit--);
}

void init_sigaction(void)
{
    struct sigaction act;

    act.sa_handler = timeout_info;
    act.sa_flags = 0;
    sigemptyset(&act.sa_mask);
    sigaction(SIGPROF, &act, NULL);
}

void init_time(void)
{
    struct itimerval val;

    val.it_value.tv_sec = 1;
    val.it_value.tv_usec = 0;
    val.it_interval = val.it_value;
    setitimer(ITIMER_PROF, &val, NULL);
}

int main(void)
{
    init_sigaction();
    init_time();

    printf("10s left\n");
    while(1);

    return 0;
}
